<?php
class CustomerModel extends Model
{
    public function getCustomers($CustomerInput)
    {
    	$whereClause = "WHERE 1=1 ";

    	if(strlen($CustomerInput['name'])>0)
    	{
    		$whereClause .= "AND name LIKE ".$this->_db->quoteLike("%".$CustomerInput['name']."%");
    	}
    	if(strlen($CustomerInput['birthday'])>0)
    	{
    		$whereClause .= "AND birthday = ".$this->_db->quoteLike($CustomerInput['birthday']);
    	}
    	if(strlen($CustomerInput['phone'])>0)
    	{
    		$whereClause .= "AND phone LIKE ".$this->_db->quoteLike("%".$CustomerInput['phone']."%");
    	}
    	if(strlen($CustomerInput['email'])>0)
    	{
    		$whereClause .= "AND email LIKE ".$this->_db->quoteLike("%".$CustomerInput['email']."%");
    	}
        return $this->_db->fetchAll("
            SELECT customer_id, name, password, birthday, phone, email, date_created
            FROM customer ".$whereClause);
    }
    public function delCustomers($stringid)
    {
    	$ids = explode(",", substr($stringid, 0,-1));
    	$whereClause = ' WHERE ';
    	foreach ($ids as $id) 
    	{
    		$whereClause .= "customer_id =".$id.' OR ';
    	}
    	$whereClause = substr($whereClause, 0, -3);
    	$this->_db->query("
    		DELETE FROM customer 
    		".$whereClause);
    }
    public function addCustomer($CustomerInput)
    {
        $valuesClause = " (".$this->_db->quoteLike($CustomerInput['add-email']).",".$this->_db->quoteLike(md5($CustomerInput['add-password']));
        $valuesClause .= ",".$this->_db->quoteLike($CustomerInput['add-name']);
        $valuesClause .= ",".$this->_db->quoteLike($CustomerInput['add-birthday']);
        $valuesClause .= ",".$this->_db->quoteLike($CustomerInput['add-phone']);
        $valuesClause .= ",'')";
        return $this->_db->query("
            INSERT INTO customer (email, password, name, birthday, phone, date_created)
             VALUES".$valuesClause); 
    }
    public function checkEmail($email)
    {
        return $this->_db->fetchOne("
            SELECT customer_id 
            FROM customer
            WHERE email =".$this->_db->quoteLike($email)." LIMIT 0,1");
    }
    public function getCustomerInfo($id)
    {
        return $this->_db->fetchRow("
            SELECT customer_id, name, birthday, phone, email, date_created
            FROM customer
            WHERE customer_id=".$id);
    }
    public function getCustomerAddress($id)
    {
        return $this->_db->fetchAll("
            SELECT customer_address_id, receiver_name, phone, address
            FROM customer_address
            WHERE customer_id=".$id);
    }
    public function updateCustomer($CustomerInput,$id)
    {
        return $this->_db->query("
            UPDATE customer 
            SET name =".$this->_db->quoteLike($CustomerInput['customer-name']).",
            phone =".$this->_db->quoteLike($CustomerInput['customer-phone']).",
            birthday =".$this->_db->quoteLike($CustomerInput['customer-birthday'])."
            WHERE customer_id=".$id);
    }
    public function deleteAddress($AddressId)
    {
        $ids = explode(",", substr($AddressId, 0,-1));
        $whereClause = ' WHERE ';
        foreach ($ids as $id) 
        {
            $whereClause .= "customer_address_id =".$id.' OR ';
        }
        $whereClause = substr($whereClause, 0, -3);
        $this->_db->query("
            DELETE FROM customer_address 
            ".$whereClause);
    }
    public function addAddress($NewAddress,$id)
    {
        return $this->_db->query("
            INSERT INTO customer_address (customer_id, receiver_name, phone, address) 
            VALUES (
                ".$id.",
                ".$this->_db->quoteLike($NewAddress['add-receiver']).",
                ".$this->_db->quoteLike($NewAddress['add-phone']).",
                ".$this->_db->quoteLike($NewAddress['add-address']).")");
    }
}